﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.Windows.Threading;
using System.Windows.Media.Animation;
using DOMercury.Interfaces;


namespace Bezel__
{
    public enum ListSelectorType
    {
        Item,
        Action,
        Param
    }

    /// <summary>
    /// Interaction logic for BezelListSelector.xaml
    /// </summary>
    public partial class ItemList : Window
    {
		bool currentlyVisible = false;
        private ListSelectorType type;

        public ItemList(ListSelectorType ListType)
        {
            InitializeComponent();
            //(BitmapSource)FindResource("diveArrow") = DOMercury.Engine.Interface.EngineCore.GetIconImageWPF("Dive Arrow"); ;
            
            type = ListType;
            itemsListBox.ItemsSource = new List<IItem>();
			MouseLeftButtonDown += new MouseButtonEventHandler(OnMouseMove);
            itemsListBox.SelectionChanged += new SelectionChangedEventHandler(itemsListBox_SelectionChanged);
           
            
        }

        void itemsListBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!CurrentlyVisible || e.AddedItems.Count == 0)
                return; 

            switch (type)
            {
                case ListSelectorType.Item:
                    DOMercuryCoreManager.Instance.FoundItemPaths = false;
                    lblItemType.Text = ((IItem)(itemsListBox.SelectedItem)).ItemType;
                    DOMercuryCoreManager.Instance.CurrentItem = ((IItem)(itemsListBox.SelectedItem)); break;
                case ListSelectorType.Action:
                    DOMercuryCoreManager.Instance.CurrentAction = ((IAction)(itemsListBox.SelectedItem)); break;
                default:
                    DOMercuryCoreManager.Instance.FoundItemPaths = false;
                    lblItemType.Text = ((IItem)(itemsListBox.SelectedItem)).ItemType;
                    DOMercuryCoreManager.Instance.CurrentParam = ((IItem)(itemsListBox.SelectedItem)); break;
            
            }
            
            if (itemsListBox.SelectedItem != null)
            {
                itemsListBox.ScrollIntoView(itemsListBox.SelectedItem);
            }

        }

        void OnMouseMove(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        public void IncrementItem()
        {
            if (itemsListBox.ItemsSource != null)
            {
                if (itemsListBox.SelectedIndex < ((List<IItem>)(itemsListBox.ItemsSource)).Count)
                {
                    itemsListBox.SelectedIndex++;
                }
            }
        }

        public void DecrementItem()
        {
            if (itemsListBox.ItemsSource != null)
            {
                if (itemsListBox.SelectedIndex > 0)
                {
                    itemsListBox.SelectedIndex--;
                }
            }
        }

        public List<IItem> ItemsSource
        {
            get
            {
                return itemsListBox.ItemsSource as List<IItem>;
            }
            set
            {
                itemsListBox.ItemsSource = value;
                if (value.Count > 0)
                {
                    itemsListBox.SelectedIndex = 0;
                }
            }
        }

        public IItem SelectedItem
        {
            get
            {
                return itemsListBox.SelectedItem as IItem;
            }
            set
            {
                itemsListBox.SelectedItem = value;
            }
			
        }

		public bool CurrentlyVisible 
        {
			get 
            {
			    return currentlyVisible;
			}
            set 
            {
                currentlyVisible = value;
                Opacity = (currentlyVisible) ? 1 : 0;

                if (currentlyVisible && !IsVisible)
                {
                    Visibility = Visibility.Visible;
                }

                if (currentlyVisible)
                {
                    Windows.FocusHelper.sendWindowToFront(this);
                    Show();
                }
                else
                {
                    Hide();
                }
                    
                    
			}
		}

        public void SetDragMove(Window parentWindow)
        {
            parentWindow.MouseDown += new MouseButtonEventHandler(parentWindow_MouseDown);
        }

        void parentWindow_MouseDown(object sender, MouseButtonEventArgs e)
        {
            this.DragMove();
        }

        internal void ClearItemsSource()
        {
            itemsListBox.ItemsSource = new List<IItem>();
        }
    }
}
